//
//  LivePusherLoginViewController.m
//  live_pusher
//
//  Created by HuJian on 2019/1/22.
//  Copyright © 2019 hywood. All rights reserved.
//

#import "LivePusherLoginViewController.h"
#import "LivePusherConfigViewController.h"
#import "AppServer.h"
#import "LivePusherDefine.h"
#import <CommonCrypto/CommonDigest.h>
@interface LivePusherLoginViewController ()

@property (strong, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet UITextField *channelId;
@property (weak, nonatomic) IBOutlet UITextField *channelPassword;
@property (weak, nonatomic) IBOutlet UIButton *enterButton;


@end

@implementation LivePusherLoginViewController


-(void) viewDidLoad{
    [super viewDidLoad];
    self.title = [@"live_scene_list" localString];
    
}

- (IBAction)enter:(id)sender {
    NSString *urlString = @"verifyPrivilege/app/";
    NSString *allUrlString = [NSString stringWithFormat:@"%@%@%@?password=%@",ServerUrl,urlString,self.channelId.text,[self MD5ForLower32Bate:self.channelPassword.text]];
    
    [AppServer getWithUrlString:allUrlString  completionHandler:^(NSString * _Nullable errString, NSDictionary * _Nullable resultDic) {
        if (errString) {
            NSLog(@"%@ error:%@", allUrlString, errString);
        }else{
            [AppServer judgmentResultDic:resultDic success:^(id dataObject) {
                NSDictionary *dataDic = (NSDictionary *)dataObject;
                LivePusherConfigViewController *pushConfigVC = [[LivePusherConfigViewController alloc] init];
                pushConfigVC.loginDic=dataDic;
                pushConfigVC.channelId=self.channelId.text;
                pushConfigVC.password=self.channelPassword.text;
                [self.navigationController pushViewController:pushConfigVC animated:YES];
                [self.channelId setText:@""];
                [self.channelPassword setText: @""];
            } doFailure:^(NSString * errorStr) {
                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:errorStr preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }];
            
        }
        
    }];
   }
- (NSString *)MD5ForLower32Bate:(NSString *)str{
    
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02x", result[i]];
    }
    
    return digest;
}

@end
